﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using yq.ControllerBase;
using yq.WebModel.Sys;

namespace yq.Controller.Sys
{
    /// <summary>
    /// 部门管理
    /// </summary>
    public class DepartmentController : BaseController
    {
        /// <summary>
        /// 部门首页 不分页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var listPage = new WebModel.ListPage<Model.Logic.DepartmentList>();
            listPage.AddRoleLinkButton("编辑", "EditDepartment", "Department");
            listPage.AddRoleLinkButton("删除", "DoDelDepartment", "Department");
            listPage.AddRoleLinkButton("添加", "AddDepartment", "Department");

            return PartialView(listPage.RoleLinkList);
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("DepartmentId")]
        [ErrorFilter("获取部门数据失败", ErrorFilterType.Text)]
        public ActionResult EditDepartment()
        {
            var info = this.GetModel<yq.Model.Logic.DepartmentList>();
            DepartmentEidt edit = new DepartmentEidt();
            edit.SetValue(info);

            return PartialView(edit);
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("DepartmentId")]
        [ErrorFilter("编辑部门数据失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoEditDepartment()
        {
            var info = this.GetModel<yq.Model.Logic.DepartmentList>();
            TryUpdateModel(info);

            //编辑
            var rv= yq.GetService.Create<yq.IService.IDepartment>().EditDepartment(info);

            return Json(rv);
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="DepartmentId">上级部门</param>
        /// <returns></returns>
        [VerifyFilter("DepartmentId")]
        public ActionResult AddDepartment(string departmentId)
        {
            DepartmentEidt info = new DepartmentEidt();
            info.SuperiorDep = departmentId;

            return PartialView(info);
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("添加部门数据失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddDepartment()
        {
            yq.Model.Logic.DepartmentList info = new Model.Logic.DepartmentList();
            TryUpdateModel(info);
            var rv = yq.GetService.Create<yq.IService.IDepartment>().AddDepartment(info);

            return Json(rv);
        }


        /// <summary>
        /// 删除部门
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("DepartmentId")]
        [ErrorFilter("删除部门数据失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoDelDepartment()
        {
            var info = this.GetModel<yq.Model.Logic.DepartmentList>();
            var rv = yq.GetService.Create<yq.IService.IDepartment>().DelDepartment(info.DepartmentId);

            return Json(rv);
        }



        /// <summary>
        /// 选择部门
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectDepartment()
        {
            return PartialView();
        }

        /// <summary>
        /// 查询部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DoSelectDep()
        {
            var list=yq.GetService.Create<yq.IService.IDepartment>().DepartmentAll();
            zTreeExt zlist = new zTreeExt();
      
            foreach (var info in list)
            {
                zlist.Add(info.DepartmentId, info.SuperiorDep, info.Title, true);

            }

            return Json(zlist.List);
        }
    }
}
